/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { Work } from '../../../../../src/base/internal/pool/model/work';
import { Priority } from '../../../../../src/base/internal/pool/enum/priority';
import { PriorityReadyQueue } from '../../../../../src/base/internal/pool/ready-queue/priority-ready-queue';

describe('test priority ready queue', () => {
  const readyQueue = new PriorityReadyQueue();
  test('test push', () => {
    const work1 = new Work(Priority.HEAVY, undefined, '1');
    const work2 = new Work(Priority.LIGHT, undefined, '2');
    readyQueue.push(work1);
    readyQueue.push(work2);
    expect(readyQueue.pop()!.getPriority() === Priority.HEAVY).toBeTruthy();
    expect(readyQueue.pop()!.getPriority() === Priority.LIGHT).toBeTruthy();
    work1.setPriority(Priority.FIRST);
    work2.setPriority(Priority.FIRST);
    readyQueue.push(work1);
    readyQueue.push(work2);
    expect(readyQueue.pop()!.getWorkPath() === '2').toBeTruthy();
    expect(readyQueue.pop()!.getWorkPath() === '1').toBeTruthy();
    work1.setPriority(Priority.LAST);
    work2.setPriority(Priority.LAST);
    readyQueue.push(work1);
    readyQueue.push(work2);
    expect(readyQueue.pop()!.getWorkPath() === '1').toBeTruthy();
    expect(readyQueue.pop()!.getWorkPath() === '2').toBeTruthy();
  });

  test('test pop', () => {
    expect(readyQueue.pop()).toBeUndefined();
    const work1 = new Work(Priority.LAST, undefined, '');
    const work2 = new Work(Priority.LIGHT, undefined, '');
    const work3 = new Work(Priority.MEDIUM, undefined, '');
    const work4 = new Work(Priority.HEAVY, undefined, '');
    const work5 = new Work(Priority.FIRST, undefined, '');
    readyQueue.push(work1);
    readyQueue.push(work2);
    readyQueue.push(work3);
    readyQueue.push(work4);
    readyQueue.push(work5);
    expect(readyQueue.pop()!.getPriority() === Priority.FIRST).toBeTruthy();
    expect(readyQueue.pop()!.getPriority() === Priority.HEAVY).toBeTruthy();
    expect(readyQueue.pop()!.getPriority() === Priority.MEDIUM).toBeTruthy();
    expect(readyQueue.pop()!.getPriority() === Priority.LIGHT).toBeTruthy();
    expect(readyQueue.pop()!.getPriority() === Priority.LAST).toBeTruthy();
  });

  test('test clear', () => {
    const work1 = new Work(Priority.LAST, undefined, '');
    const work2 = new Work(Priority.LIGHT, undefined, '');
    readyQueue.push(work1);
    readyQueue.push(work2);
    readyQueue.clear();
    expect(readyQueue.pop()).toBeUndefined();
  });

  test('test update', () => {
    const work1 = new Work(Priority.LAST, undefined, '1');
    const work2 = new Work(Priority.LIGHT, undefined, '2');
    readyQueue.push(work1);
    readyQueue.push(work2);
    work1.setPriority(Priority.FIRST);
    readyQueue.update(work1);
    expect(readyQueue.pop()!.getPriority() === Priority.FIRST).toBeTruthy();
    expect(readyQueue.pop()!.getPriority() === Priority.LIGHT).toBeTruthy();
  });

  test('test update', () => {
    expect(readyQueue.empty()).toBeTruthy();
    const work1 = new Work(Priority.LAST, undefined, '1');
    const work2 = new Work(Priority.LIGHT, undefined, '2');
    readyQueue.push(work1);
    readyQueue.push(work2);
    expect(readyQueue.empty()).toBeFalsy();
    readyQueue.clear();
    expect(readyQueue.empty()).toBeTruthy();
  });
});
